<template>
  <div class="topbar">
    <div class="wrap topbarwrap">
      <div class="left">欢迎来到叩丁狼积分商城</div>
      <ul class="right">
        <li class="imgs">
          <img :src="userInfo.headImg" alt="" class="userImg" />
        </li>
        <li>用户名:{{ userInfo.nickName }}</li>
        <li>我的积分:{{ userInfo.coin }}</li>
        <li>获取积分</li>
        <li>叩丁狼官网</li>
        <li class="cart-btn" v-if="isLogined">
          <img src="../assets/img/cart.png" alt="" />
          <span>购物车</span>
          <strong>{{ cartTotal }}</strong>
        </li>
        <li class="lgoin-btn" v-else @click="changeIsShowLoginModal(true)">
          登录
        </li>
      </ul>
    </div>
  </div>
</template>

<script>
import { mapMutations, mapState, mapActions } from "vuex";
import { WeiXinLoginAPI } from "@/request/api.js";
export default {
  data() {
    return {};
  },
  computed: {
    ...mapState({
      isLogined: (state) => state.loginStatus.isLogined,
      userInfo: (state) => state.userInfo.userInfo,
      cartTotal: (state) => state.userInfo.cartTotal,
    }),
  },
  methods: {
    ...mapMutations({
      changeIsShowLoginModal: "showModal/changeIsShowLoginModal",
      changeIsLogined: "loginStatus/changeIsLogined",
      initUserInfo: "userInfo/initUserInfo",
    }),
    ...mapActions({
      asyncShowToast: "showToast/asyncShowToast",
      asyncUserInfo: "userInfo/asyncUserInfo",
    }),
  },
  created() {
    //拿到响应的code值,如果有向后台发送请求判断code
    this.$router.onReady(async () => {
      console.log("ready-code:", this.$route.query.code);
      let pathCode = this.$route.query.code;
      if (pathCode) {
        let res = await WeiXinLoginAPI({
          code: pathCode,
        });
        console.log("在请求中打印", res);
        //判断后台返回的响应如果想的code值为0,做登录相关操作
        if (res.data.code == 0) {
          //提示登录
          this.asyncShowToast({
            isShow: true,
            title: res.data.message,
            type: "success",
          });
          //设置token
          localStorage.setItem("x-auth-token", res.data["x-auth-token"]);
          //更新登录状态,
          this.changeIsLogined(true);
          //清除地址栏中的code
          this.$router.push(this.$route.path);
        } else if (res.data.code == 400) {
          console.log("400", res);
          //登录失败提示框
          this.asyncShowToast({
            isShow: true,
            title: res.data.message,
            type: "danger",
          });
          eventBus.$emit("wx", true);
          //自动打开登录框
          this.changeIsShowLoginModal(true);
        } else if (res.data.code == 407) {
          this.asyncShowToast({
            isShow: true,
            title: res.data.message,
            type: "danger",
          });
          //重新请求该地址
          this.$router.push(this.$route.path);
          //打开登录框
          this.changeIsShowLoginModal(true);
          //储存uuid
          localStorage.setItem("uuid", res.data.uuid);
        }
      } else {
        //判断是否有token如果有显示购物车
        let token = localStorage.getItem("x-auth-token");
        this.changeIsLogined(Boolean(token));
        //刷新页面时判断token
        if (token) {
          //获取用户信息方法
          this.asyncUserInfo();
        } else {
          //初始化用户信息
          this.initUserInfo();
        }
      }
    });
  },
};
</script>

<style lang="less" scoped>
.topbar {
  height: 40px;
  line-height: 40px;
  background: #242b39;
  color: #fff;
  font-size: 14px;
  .topbarwrap {
    display: flex;
    justify-content: space-between;
    text-align: center;
    font-size: 0;
    .right {
      li {
        font-size: 14px;
        float: left;
        margin-left: 20px;
        &.lgoin-btn {
          width: 124px;
          background: #0a328e;
        }
        &.cart-btn {
          width: 124px;
          height: 40px;
          background: #0a328e;
          text-align: center;
          img {
            width: 20px;
            height: 19px;
            vertical-align: middle;
          }
          span {
            margin: 0 8px;
          }
          strong {
            width: 22px;
            height: 22px;
            line-height: 22px;
            display: inline-block;
            border-radius: 50%;
            background-color: #fd604d;
          }
        }
      }
      .imgs {
        font-size: 0;
      }
      .userImg {
        width: 23px;
        height: 22px;
        border-radius: 13px;
        vertical-align: middle;
      }
    }
  }
}
</style>